//
//  AuthTFView.swift
//  认证 - TextField
//
//  Created by 千策万划-mini on 2025/5/6.
//

import Foundation

class AuthTFView: UIView, UITextFieldDelegate {

    var model: ModelAuthPersonalItemInfo = ModelAuthPersonalItemInfo()
    var blockValueChange:((ModelAuthPersonalItemInfo) -> Void)?
    
    // MARK: - lazy
    lazy var labelTitle: UILabel = {
        let result = UILabel()
        result.font = UIFont.customSemiBoldFont(size: 16)
        result.textColor = UIColor(hex: "#5228D7")
        return result
    }()
    lazy var imageContentBg: UIButton = {
        let result = UIButton()
        result.isUserInteractionEnabled = true
        result.setBackgroundImage(UIImage(named: "icon_login_agree_bg"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var tfContent: UITextField = {
        let result = UITextField()
        result.font = UIFont.customBoldFont(size: 14)
        result.textColor = COLOR_WHITE()
        result.textAlignment = .left
        result.isHidden = true
        result.delegate = self
        result.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return result
    }()
    lazy var labelContent: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()

    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        widthHeight = XY(SCREEN_WIDTH - W(35) * 2, 0)
        backgroundColor = .clear
        addSubView()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 添加
    func addSubView() {
        addSubview(labelTitle)
        addSubview(imageContentBg)
        imageContentBg.addSubview(tfContent)
        imageContentBg.addSubview(labelContent)

        resetView()
    }

    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            tfContent.isHidden = false
            labelContent.isHidden = true
            tfContent.becomeFirstResponder()
            break
        case 2:

            break
        default:
            break
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        showContent()
        return true
    }

    // 实现 textFieldDidEndEditing 方法，监听失去焦点事件
    func textFieldDidEndEditing(_ textField: UITextField) {
        showContent()
    }
    
    @objc func textFieldDidChange(_ textField: UITextField) {
        self.model.pounding = textField.text ?? ""
        self.model.wisest = textField.text ?? ""
        if let block = blockValueChange {
            block(self.model)
        }
    }
    
    func showContent() {
        labelContent.resetView(
            title: tfContent.text ?? "",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084"),
            widthMark: imageContentBg.width - W(15) * 2
        )
        labelContent.leftCenterY = XY(W(15), imageContentBg.height/2)
        tfContent.isHidden = isStr(tfContent.text ?? "")
        labelContent.isHidden = !tfContent.isHidden
    }

    // MARK: - 重置界面
    func resetView(model: ModelAuthPersonalItemInfo = ModelAuthPersonalItemInfo()) {
        self.model = model
        labelTitle.fitTitle(UnPackStr(model.whose), variable: 0)
        labelTitle.leftCenterY = XY(0, W(50) / 2)

        imageContentBg.widthHeight = XY(width, W(50))
        imageContentBg.centerXTop = XY(width / 2, W(50))
        
        tfContent.attributedPlaceholder = NSAttributedString(
            string: UnPackStr(model.itself),
            attributes: [
                .foregroundColor: UIColor(hex: "#2D0084"),
            ]
        )
        if model.box == 1 {
            // 数字键盘
            tfContent.keyboardType = .numberPad
        }else{
            // 全键盘
            tfContent.keyboardType = .default
        }
        tfContent.text = model.pounding
        labelContent.isHidden = !isStr(model.pounding)
        tfContent.isHidden = isStr(model.pounding)
        if isStr(model.pounding) {
            labelContent.resetView(
                title: model.pounding,
                textColor: COLOR_WHITE(),
                font: UIFont.customBoldFont(size: 14),
                shadowColor: UIColor(hex: "#2D0084"),
                outlineColor: UIColor(hex: "#2D0084"),
                widthMark: imageContentBg.width - W(15) * 2
            )
            labelContent.leftCenterY = XY(W(15), imageContentBg.height/2)
        }
        
        tfContent.widthHeight = XY(imageContentBg.width - W(15) * 2, imageContentBg.height - 15)
        tfContent.leftCenterY = XY(W(15), imageContentBg.height / 2)
        height = imageContentBg.bottom
    }
}
